package cn.csc.module.member.controller.app.role;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.member.controller.app.role.vo.*;
import cn.csc.module.member.convert.role.MemberRoleConvert;
import cn.csc.module.member.dal.dataobject.role.MemberRoleDO;
import cn.csc.module.member.service.role.MemberRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.csc.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 会员角色")
@RestController
@RequestMapping("/member/role")
@Validated
public class AppMemberRoleController {

    @Resource
    private MemberRoleService roleService;

    @PostMapping("/create")
    @ApiOperation("创建会员角色")
    public CommonResult<BigInteger> createRole(@Valid @RequestBody AppMemberRoleCreateReqVO createReqVO) {
        return success(roleService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新会员角色")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody AppMemberRoleUpdateReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除会员角色")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    public CommonResult<Boolean> deleteRole(@RequestParam("id") BigInteger id) {
        roleService.deleteRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得会员角色")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    public CommonResult<AppMemberRoleRespVO> getRole(@RequestParam("id") BigInteger id) {
        MemberRoleDO role = roleService.getRole(id);
        return success(MemberRoleConvert.INSTANCE.convert(role));
    }

    @GetMapping("/getCurrentUser")
    @ApiOperation("获得当前用户的会员角色")
    public CommonResult<List<AppMemberRoleRespVO>> getCurrentUser() {
        List<MemberRoleDO> roles = roleService.getByUserId(BigInteger.valueOf(getLoginUserId()));
        return success(MemberRoleConvert.INSTANCE.convertList(roles));
    }

    @GetMapping("/list")
    @ApiOperation("获得会员角色列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<AppMemberRoleRespVO>> getRoleList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<MemberRoleDO> list = roleService.getRoleList(ids);
        return success(MemberRoleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得会员角色分页")
    public CommonResult<PageResult<AppMemberRoleRespVO>> getRolePage(@Valid AppMemberRolePageReqVO pageVO) {
        PageResult<MemberRoleDO> pageResult = roleService.getRolePage(pageVO);
        return success(MemberRoleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出会员角色 Excel")
    @OperateLog(type = EXPORT)
    public void exportRoleExcel(@Valid AppMemberRoleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberRoleDO> list = roleService.getRoleList(exportReqVO);
        // 导出 Excel
        List<AppMemberRoleExcelVO> datas = MemberRoleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员角色.xls", "数据", AppMemberRoleExcelVO.class, datas);
    }

}
